package com.minisoft.utils;

import cn.hutool.core.lang.Console;

import java.util.ArrayList;
import java.util.List;

public class LocationUtils {

    // 定义一个地点类
    static class Location {
        String name;
        /**
         * 纬度
         */
        double latitude;
        /**
         * 经度
         */
        double longitude;

        Location(String name, double longitude,double latitude) {
            this.name = name;
            this.latitude = latitude;
            this.longitude = longitude;
        }
    }

    // 初始化一些地点
    private List<Location> locations = new ArrayList<>();

    public LocationUtils() {
        locations.add(new Location("华发综合大楼", 113.5327204, 22.2146354)); // Example: Los Angeles, CA
        locations.add(new Location("华发商都", 113.5164341, 22.2306531));  // Example: New York, NY
    }

    public static void main(String[] args) {
        LocationUtils utils = new LocationUtils();
        //中国石油
        System.out.println(utils.checkLocation(113.5135326, 22.2311592, 0.5));
        //世纪城
        System.out.println(utils.checkLocation(113.5328551, 22.2175547, 0.5));
        //过了昌盛大桥
        System.out.println(utils.checkLocation(113.5457907, 22.2191732, 0.5));

        double v = utils.calculateDistance(22.215394, 113.533001, 22.211564, 113.521233);
        Console.log("distance = {}",v);
    }
    // 检查给定的经纬度是否在某个地点的附近
    public String checkLocation(double longitude, double latitude, double radius) {
        for (Location location : locations) {
            double distance = calculateDistance(latitude, longitude, location.latitude, location.longitude);
            Console.log("distance = {}",distance);
            if (distance <= radius) {
                return "You are near " + location.name;
            }
        }
        return "You are not near any predefined location.";
    }

    // Haversine公式计算两个经纬度之间的距离
    public double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        final int R = 6371; // 地球半径，单位为公里
        double latDistance = Math.toRadians(lat2 - lat1);
        double lonDistance = Math.toRadians(lon2 - lon1);
        double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
                + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
                * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return R * c; // 返回距离，单位为公里
    }
}